# -*- coding:utf-8 -*-

from . import project
from flask import render_template,request,flash,redirect,url_for,session
from admin_model import User
from flask_wtf.csrf import generate_csrf

import sys
reload(sys)
sys.setdefaultencoding('utf8')

@project.route('/login',methods=['POST','GET'])
def login():
    if request.method=='GET':
        token = generate_csrf()
        return render_template('re_login.html', token=token)

    username=request.form.get('userName')
    userpwd=request.form.get('userPwd')
    if not all([userpwd,username]):
        flash('参数不完整')
        token=generate_csrf()
        return render_template('re_login.html',token=token)
    user_list=User.query.filter_by(name=username).all()
    if user_list == []:
        flash('用户不存在')
        return redirect(url_for('project_1.login'))
    user_info = user_list[0]

    if username != user_info.name or userpwd != user_info.pwd:
        flash('用户或者密码错误')
        token=generate_csrf()
        return render_template('re_login.html',token=token)
    else:
        session['user_id']=user_info.id
        flash("登陆成功！")
        return redirect(url_for('project_1.index'))















